#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    connect(&dirPb,SIGNAL(clicked()),this,SLOT(clickDirPb()));
    connect(&savePb,SIGNAL(clicked()),this,SLOT(clickSavePb()));
    connect(&closePb,SIGNAL(clicked()),this,SLOT(clickClosePb()));
    connect(&cBox,SIGNAL(activated(int)),this,SLOT(clickcBox()));

    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::clickDirPb()
{
    dir = QFileDialog::getExistingDirectory(this, tr("Open "),"", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks)  ;




    QDir direc(dir);
    QStringList filters;


    filters <<  "*.txt";
    direc.setNameFilters(filters);

    QStringList sList = direc.entryList(QDir::Files);

    cBox.addItems(sList);

}
void MainWindow::clickcBox()
{
    qs = cBox.currentText();
    QFile pFile(dir + '/' + qs);
    if (!pFile.open(QIODevice::ReadOnly | QIODevice::Text))
             return;

    QByteArray line = pFile.readAll();
    tEdit.setText(line);
    pFile.close();
}

void MainWindow::clickClosePb()
{
   this->close();
}

void MainWindow::clicksavePb()
{
    QString file = tEdit.toPlainText();
    QFile pFile(dir + '/' + qs);
    if (!pFile.open(QIODevice::WriteOnly | QIODevice::Text))
             return;
    pFile.write((const char *)file.toAscii().data());
    pFile.close();

}
